package com.alumni_circle.core.web.rbac;

import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.entity.rbac.Role;
import com.alumni_circle.core.entity.rbac.RoleAuthority;
import com.alumni_circle.core.entity.rbac.VO.RoleInsertVO;
import com.alumni_circle.core.entity.rbac.VO.RoleSearchVO;
import com.alumni_circle.core.entity.rbac.VO.RoleUpdateVO;
import com.alumni_circle.core.entity.rbac.VO.RoleViewVO;
import com.alumni_circle.core.service.rbac.IRoleService;
import com.alumni_circle.core.service.rbac.impl.RoleAuthorityServiceImpl;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.me.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;

/**
 * @author 龙小虬
 * @since 2020-08-02 20:43
 */
@Api(tags = "角色管理")
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController<Role> {

    @Resource
    IRoleService roleService;

    @Resource
    RoleAuthorityServiceImpl roleAuthorityService;

    /**
     * 增加角色信D:\系统桌面\project\ant-design\ant\src\core息
     * @param roleInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "增加角色信息", notes = "增加角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="增加角色信息",url="/role/addRole")
    public ResponseModel addRole(@ApiParam(required = true, name = "roleInsertVO")
                                 @RequestBody RoleInsertVO roleInsertVO) {
       User user = SecurityUtils.getUser();
       Role role = ModelUtils.transform(roleInsertVO,Role.class);
       role.setCreateBy(user.getId());
       role.setCreateName(user.getName());
       roleService.save(role);
       // 所有新增的角色都对应普通权限
        RoleAuthority roleAuthority = new RoleAuthority();
        roleAuthority.setRoleId(role.getId());
        roleAuthority.setAuthorityId("1294205559630819330");
       roleAuthorityService.save(roleAuthority);
       return new ResponseModel<>(role.getId());
    }

    /**
     * 根据id查询角色信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询角色信息", notes = "查询角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询角色信息",url="/role/selectRole")
    public ResponseModel selectRole(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {
        Role role = roleService.getById(baseId.getId());
        return new ResponseModel<>(role);
    }

    /**
     * 根据id删除角色信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除角色信息", notes = "删除角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除角色信息",url="/role/deleteRole")
    public ResponseModel deleteRole(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {
        roleService.removeById(baseId.getId());
        return new ResponseModel<>(baseId.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/disRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "禁用角色信息", notes = "禁用角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="禁用角色信息",url="/role/disRole")
    public ResponseModel disRole(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {
        Role role = new Role();
        role.setState("1");
        role.setId(baseId.getId());
        roleService.updateById(role);
        return new ResponseModel<>(baseId.getId());
    }

    /**
     * 根据id更新角色信息
     * @param roleUpdateVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新角色信息", notes = "更新角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新角色信息",url="/role/updateRole")
    public ResponseModel updateRole(@ApiParam(required = true, name = "roleUpdateVO")
                                    @RequestBody RoleUpdateVO roleUpdateVO) {
        User user = SecurityUtils.getUser();
        Role role = ModelUtils.transform(roleUpdateVO,Role.class);
        role.setCreateBy(user.getId());
        role.setCreateName(user.getName());
        roleService.updateById(role);
        return new ResponseModel<>(roleUpdateVO.getId());
    }


    /**
     * 查询所有角色信息 ，可分页查询
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有角色信息", notes = "查询所有角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询所有角色信息",url="/role/getAllRole")
    public ResponseListModel getAllRole(@ApiParam(required = true,name = "pagebase")
                                        @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<RoleViewVO> list = ModelUtils.copyListProperties(roleService.list(), RoleViewVO.class);
        PageInfo<RoleViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    @ResponseBody
    @RequestMapping(value = "/searchRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索所有角色信息", notes = "查询所有角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索所有角色信息",url="/role/searchRole")
    public ResponseListModel searchRole(@ApiParam(required = true,name = "pagebase")
                                        @RequestBody RoleSearchVO roleSearchVO) {
        PageHelper.startPage(roleSearchVO.getPageNum(),roleSearchVO.getPageSize());
        List<RoleViewVO> list = ModelUtils.copyListProperties(roleService.list(new QueryWrapper<Role>()
                                                                          .like("update_time",roleSearchVO.getUpdateTime())
                                                                           .like("update_name",roleSearchVO.getUpdateName())
                                                                          .like("state",roleSearchVO.getState())
                                                                          .like("role_name",roleSearchVO.getRoleName())),
                RoleViewVO.class);
        PageInfo<RoleViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    @ResponseBody
    @RequestMapping(value = "/getRouting", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询角色的路由表", notes = "查询角色的路由表", httpMethod = "GET", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询角色的路由表",url="/role/getRouting")
    public ResponseListModel getRouting() {
        return new ResponseListModel<>();
    }

}
